package com.wolfalpha.utron.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.InformationDetailActivity;
import com.wolfalpha.utron.adapter.ItemInformationAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.bean.ItemInformation;
import com.wolfalpha.utron.service.InformationService;
import com.wolfalpha.utron.service.impl.InformationServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.list.WaterDropListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodon on 15/8/17.
 */
public class MainInformation extends Fragment implements WaterDropListView.IWaterDropListViewListener {
    //对父Activity的接口
    private MainInformationListener mlistener;

    private WaterDropListView listView;     //列表

    private List<ItemInformation> informations;
    private ItemInformationAdapter adapter;//适配器

    private InformationService informationService;

    private int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                informations = informationService.getList(page);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (informations != null) {
                            adapter.setData(informations);
                            if (informations.size() < AppConfig.LIST_PAGE_SIZE) {
                                listView.setPullLoadEnable(false);
                            } else {
                                listView.setPullLoadEnable(true);
                            }
                        } else {
                            UIHelper.ToastMessage(getActivity(), "没有数据");
                            adapter.setData(new ArrayList<ItemInformation>());
                            listView.setPullLoadEnable(false);
                        }
                        listView.stopRefresh();
                    }
                });
            }
        });
    }

    @Override
    public void onLoadMore() {
        try {
            ThreadPoolUtils.execute(new Runnable() {
                @Override
                public void run() {
                    final List<ItemInformation> itemInformations = informationService.getList(++page);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (itemInformations != null) {
                                informations.addAll(itemInformations);
                                adapter.setData(informations);
                                if (itemInformations.size() < AppConfig.LIST_PAGE_SIZE) {
                                    listView.setPullLoadEnable(false);
                                } else {
                                    listView.setPullLoadEnable(true);
                                }
                            } else {
                                page--;
                                UIHelper.ToastMessage(getActivity(), "没有数据");
                                listView.setPullLoadEnable(false);
                            }
                            listView.stopLoadMore();
                        }
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface MainInformationListener {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_information_list, container, false);

        listView = (WaterDropListView) view.findViewById(R.id.w_main);
        informationService = new InformationServiceImpl();
        listView.setWaterDropListViewListener(this);
        listView.setPullLoadEnable(false);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > informations.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("infoId", informations.get(position - 1).getId());
                    UIHelper.showActivity(getActivity(), InformationDetailActivity.class, bundle);
                }
            }
        });
        initData();
        return view;
    }

    /**
     * 加载数据
     */
    private void initData() {
        informations = new ArrayList<ItemInformation>();
        adapter = new ItemInformationAdapter(getActivity(), informations);
        listView.setAdapter(adapter);
        onRefresh();
    }

    public void setMainInformationListener(MainInformationListener listener) {
        this.mlistener = listener;
    }
}
